package com.jt.config;

import java.io.IOException;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@PropertySource("classpath:config.properties")
@ComponentScan(value="com.jt",excludeFilters={@Filter(type=FilterType.ANNOTATION,classes={Controller.class})})
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class AppRootConfig {
	@Bean
	public PropertySourcesPlaceholderConfigurer newprsoplcig(){
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
		return propertySourcesPlaceholderConfigurer;
	}
	@Scope("singleton")
	/**配置Druid*/
	@Bean(value="dataSource",initMethod="init",destroyMethod="close")
	public DruidDataSource dataSource(
			@Value("${jdbcDriver}")String driver,
			@Value("${jdbcUrl}")String url,
			@Value("${jdbcUsername}")String user,
			@Value("${jdbcPassword}")String password){
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(driver);
		dataSource.setUrl(url);
		dataSource.setUsername(user);
		dataSource.setPassword(password);
		return dataSource;
	}
	/**配置sqlSessionFactoryBean
	 * @throws IOException */
	@Bean("sqlSessionFactory")
	public SqlSessionFactoryBean sqlSessionFactoryBean (@Autowired DataSource dataSource) throws IOException{
		SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
		sessionFactoryBean.setDataSource(dataSource);
//		Resource[] mapperLocations = {new ClassPathResource("mapper/sys/SysConfigMapper.xml")};
		Resource[] mapperLocations = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sys/*.xml");
		sessionFactoryBean.setMapperLocations(mapperLocations);
		return sessionFactoryBean;
	}
	@Bean("scannerConfigurer")
	public MapperScannerConfigurer scannerConfigurer(){
		MapperScannerConfigurer msc=new MapperScannerConfigurer();
		msc.setBasePackage("com.jt.**.dao");
		msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
		return msc;
	}
	@Bean("txManager")
	public DataSourceTransactionManager newDataSourceTransactionManager(DataSource dataSource){
		DataSourceTransactionManager tManager=new DataSourceTransactionManager();
		tManager.setDataSource(dataSource);
		return tManager;
	}
	
}
